﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Objects;  
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataAccessLayer.Models;

namespace PresentationLayerMVC.Controllers
{
    public class ApplicantController : BaseController
    {
        ICustRepository repo;

        //
        // GET: /Applicant/

        public ActionResult Index()
        {
            IEnumerable<Customer> customers = repo.FindAllCustomers();
            return View();
        }

        //
        // GET: /Applicant/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Applicant/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Applicant/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Applicant/Edit/5

        public ActionResult Edit(int customerId)
        {
            Customer c = repo.GetCustomer(customerId);
            return View(c);
        }

        //
        // POST: /Applicant/Edit/5

        [HttpPost]
        public ActionResult Edit(int customerId, FormCollection collection)
        {
            try
            {
                Customer c = repo.GetCustomer(customerId);
                UpdateModel(c);
                repo.Save();
                return RedirectToAction("Index");   
            }            
            catch (OptimisticConcurrencyException ex)
            {
                ObjectStateEntry entry = ex.StateEntries[0];
                Customer customer = entry.Entity as Customer;
                Console.WriteLine("Failed to save {0}'s data because it was changed in the database", customer.Name);
            }

            return View();
        }

        //
        // GET: /Applicant/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Applicant/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
